package com.sunwayworld.basemodule.business.operationbatch.service.impl;

import com.sunwayworld.framework.data.page.Page;
import com.sunwayworld.framework.mybatis.mapper.MapperParameter;
import com.sunwayworld.framework.mybatis.page.PageRowBounds;
import com.sunwayworld.framework.support.PersistableHelper;
import com.sunwayworld.framework.utils.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.business.operationbatch.dao.LimsOrderTaskSupplementDao;
import com.sunwayworld.basemodule.business.operationbatch.bean.LimsOrderTaskSupplementBean;
import com.sunwayworld.basemodule.business.operationbatch.service.LimsOrderTaskSupplementService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsOrderTaskSupplementServiceImpl implements LimsOrderTaskSupplementService {

    @Autowired
    private LimsOrderTaskSupplementDao limsOrderTaskSupplementDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsOrderTaskSupplementDao getDao() {
        return limsOrderTaskSupplementDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsOrderTaskSupplementBean limsOrderTaskSupplement = jsonWrapper.parseUnique(LimsOrderTaskSupplementBean.class);
        limsOrderTaskSupplement.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsOrderTaskSupplement);
        return limsOrderTaskSupplement.getId();
    }

    @Override
    public Page<LimsOrderTaskSupplementBean> selectOrderTaskSupplementToColumns(RestJsonWrapperBean wrapper) {
        wrapper.removePageParams();
        MapperParameter parameter = wrapper.extractMapFilter();
        PageRowBounds rowBounds = wrapper.extractPageRowBounds();
        return selectPagination(() -> getDao().selectOrderTaskSupplementToColumns(parameter), rowBounds);
    }

    @Override
    public void updateSupplementByOrderTask(Long orderTaskId, RestJsonWrapperBean wrapper) {
        LimsOrderTaskSupplementBean orderTaskSupplementBean = wrapper.parseUnique(LimsOrderTaskSupplementBean.class);
        orderTaskSupplementBean.setOrderTaskId(orderTaskId);
        getDao().update(orderTaskSupplementBean, ArrayUtils.asList("fieldValue"), "orderTaskId", "fieldName");
    }
}
